package com.lily.aidouzixun.webview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.ProgressBar;

import com.lily.aidouzixun.R;
import com.lily.aidouzixun.message.MessageDispatcher;
import com.lily.aidouzixun.message.MessageType;
import com.lily.aidouzixun.ui.MyWebView;
import com.lily.aidouzixun.ui.TitleBar;
import com.lily.aidouzixun.util.L;
import com.lily.aidouzixun.util.MyConstant;

/**
 * 自定义的WebViewActivity
 * 传参：url
 *      title
 *      showback
 */
public class MyWebViewActivity extends FragmentActivity {
    private static final String TAG = "MyWebViewActivity";
    private TitleBar mTitlebar;
    private ProgressBar mProgressBar;
    private MyWebView myWebView;

    private Activity mActivity;
    private Context mContext;

    private MyWebViewFragment myWebViewFragment;

    private String curUrl = ""; //当前url
    private String title = "";  //Fragent标题

    private String backTitle = "";  //返回标题
    private boolean showBack = false;   //是否显示关闭

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.framelayout_single);
        getIntentData();
        addWebViewFragment();
    }

    /**
     * 得到Intent传递过来的参数
     */
    public void getIntentData() {
        Intent intent = getIntent();
        if (intent != null) {
            curUrl = intent.getStringExtra(MyConstant.IntentKey.WEBVIEW_URL);
            title = intent.getStringExtra(MyConstant.IntentKey.TITLE);
            backTitle = intent.getStringExtra(MyConstant.IntentKey.BACK_TITLE);
            showBack = intent.getBooleanExtra(MyConstant.IntentKey.WEBVIEW_SHOWBACK, false);
        }
    }

    /**
     * 添加WebViewFragment
     */
    private void addWebViewFragment() {
        try {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            myWebViewFragment = new MyWebViewFragment();
            Bundle args = new Bundle();
            args.putString(MyConstant.IntentKey.WEBVIEW_URL, curUrl);
            args.putString(MyConstant.IntentKey.TITLE, title);
            args.putString(MyConstant.IntentKey.BACK_TITLE, backTitle);
            args.putBoolean(MyConstant.IntentKey.WEBVIEW_SHOWBACK, showBack);
            myWebViewFragment.setArguments(args);
            fragmentTransaction.replace(R.id.fl_content, myWebViewFragment);
            fragmentTransaction.commitAllowingStateLoss();
        } catch (Exception e) {
            L.e(TAG, "addWebViewFragment-->error!");
        }
    }
    /**
     * 启动一个WebViewActivity
     * @param context
     * @param url
     * @param title
     * @param backTitle
     * @param showBack
     */
    public static void startWebViewActivity(Context context, String url, String title, String backTitle, boolean showBack) {
        Intent intent = new Intent(context, MyWebViewActivity.class);
        intent.putExtra(MyConstant.IntentKey.WEBVIEW_URL, url);
        intent.putExtra(MyConstant.IntentKey.TITLE, title);
        intent.putExtra(MyConstant.IntentKey.BACK_TITLE, backTitle);
        intent.putExtra(MyConstant.IntentKey.WEBVIEW_SHOWBACK, showBack);
        context.startActivity(intent);
    }

    /**
     * WebView 页面对物理回退键的监听
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            MessageDispatcher.sendMessage(myWebViewFragment.getHandler(), MessageType.ON_KEY_DOWN);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
